/**
*
* Tâche apériodique
*  Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN
* see <a href="{@docRoot}/Documentation/task.html"> for more information</a>
* 
* @author: Richard
* @version 3.2
*/

package storm.Tasks;

import java.util.Iterator;

import storm.Entity;
import storm.EvtContext;
import storm.Timer;
import storm.Tasks.Task.Activation;
import log.TaskTrace;
/**
 * 
 * @author richard
 *
 */
public class ARTask extends Task {
	/**
	 * 
	 */
	public ARTask(){
	}
	/**
	 * 
	 */
	protected void inRunning() {
		this.debug_mess( "[evt  TASK  STASK RUNNING]      " +this+ " "+ this.ET + " "+ this.getRET(),10);
		this.setEverRunning(true);
		
		
// MAJ de l'activation FIFO
		
		/**
		 * begin
		 */
		if(  (this.ET ==1 ) && (this.getRET()>=0 )) {
			Iterator I= this.ActivationFIFO.iterator();
			while (I.hasNext()) {
				Activation A = (Activation) I.next();
				if (A.getDeb_exec()< 0 ){
					A.setDeb_exec(this.Kernel.getTime());
					break;
				}
			}
		}
		/** 
		 * end
		 */
		if(  (this.ET > 0 ) && (this.getRET()== 0)) {
			Iterator I= this.ActivationFIFO.iterator();
			while (I.hasNext()) {
				Activation A = (Activation) I.next();
				if (A.getEnd_exec()< 0 ){
					A.setEnd_exec(this.Kernel.getTime());
					break;
				}
				}
		}
		
		
		if(  (this.ET > 0 ) && (this.getRET()== 0)) {
			/**
			 * END TASK 
			 */
			
			this.debug_mess( "[END STASK ",10);
			//this.setUnexisting();$
			if (this.getOwnFieldStringValue("autorun")!=null){
				if (this.getOwnFieldStringValue("autorun").equalsIgnoreCase("false")) this.raiseBlock();
				else {
					this.initRET();
			         this.ActivationFIFO.addLast(new Activation(this.Kernel.getTime()+1));

				}
			}
			else {
				this.initRET();
		         this.ActivationFIFO.addLast(new Activation(this.Kernel.getTime()+1));

			}
//			this.RET = 10 ;
//			this.EET = 0;
			//this.AET = this .RET = this.calcAET.getAET(this.BCET,this.WCET);
		
			
			this.raiseBlock(this);
			this.finished();
	        this.raiseUnBlock(this);
	        Timer timer = new Timer("resetRun", 1 ,this);
	        
	        
	        
	        
	        


		}
       
		//System.out.println("AV " + this.Kernel.getTime() + " --  " + this.getEET() + " --  " + this.getRET());

	}
	
	/**
	 * 
	 */
	public void onUnBlock(	EvtContext c) {
			//super.onUnBlock(c);
		     //this.initRET();
		this.Kernel.getLogs().addTrace(new TaskTrace(this.Kernel.getTime(),this, TaskTrace.Type.UB_B_LOCK,this.blockSize()));
	

	}
	/**
	 * 
	 */
	public void onUnBlock(Entity A, EvtContext c) {
	

		//System.out.println("AV " + this.Kernel.getTime() + " --  " + this.getEET() + " --  " + this.getRET());

		this.debug_mess("[Task is unBLOCKED] by " + A  ,10);
		if(  (this.ET > 0 ) && (this.getRET()== 0)) {
			this.initRET();
		}
		this.removeBlock(A);
	
		 if (this.blockSize() == 1 ) {
			 	this.removeBlock(this);
			 	if (this.getRET() == 0 ) this.initRET();
		 }
			this.Kernel.getLogs().addTrace(new TaskTrace(this.Kernel.getTime(),this, TaskTrace.Type.UB_B_LOCK,this.blockSize()));

	}
	
	public void onBlock( EvtContext c) {
		
		this.debug_mess("[Task is BLOCKED] by "  +this  ,10);
		if(  (this.ET > 0 ) && (this.getRET()== 0)) {
			this.initRET();
		}
		this.addBlock(this);
		this.Kernel.getLogs().addTrace(new TaskTrace(this.Kernel.getTime(),this, TaskTrace.Type.UB_B_LOCK,this.blockSize()));

	}
	/**
	 * 
	 */
	public void onBlock(Entity A, EvtContext c) {
	//	System.out.println( this.id + "   " + this.Kernel.getTime() + " --  " + this.blockSize() + " --  ");

		this.debug_mess("[Task is BLOCKED] by " + A  ,10);
	 	if (this.getRET() == 0 ) this.initRET();
		this.addBlock(A);
	}
	/**
	 * 
	 */
	public void onActivate(EvtContext c) {
		super.onActivate(c);
		   this.ActivationFIFO.addLast(new Activation(this.Kernel.getTime()));
		this.debug_mess("[Task is ACTIVATE] " + this ,10);  
		
			if (this.Blocks.size() > 0 ) this.setWaiting();
			this.initRET();
			this.Kernel.getLogs().addTrace(new TaskTrace(this.Kernel.getTime(),this, TaskTrace.Type.ACTIVATION));

	}
	
	
	   public void resetRun(){
			this.setEverRunning(false);
			
		}
	
}
